<template>
    <el-dialog v-model="isShow" title="修改轮播图" draggable destroy-on-close append-to-body @open="openDialog">
        <div class="flex_str_str itemSty">
            <div class="nameSty">名称:</div>
            <el-input v-model="params.name" class="itpSty"></el-input>
        </div>
        <div class="flex_str_str itemSty">
            <div class="nameSty">类型:</div>
            <el-input v-model="params.type" class="itpSty" type="number"></el-input>
        </div>
        <div class="flex_str_str itemSty">
            <div class="nameSty">排序:</div>
            <el-input v-model="params.sort" class="itpSty" type="number"></el-input>
            <span style="font-size: 12px;margin-top:8px;">(数字越小越靠前)</span>
        </div>
        <div class="flex_str_str itemSty">
            <div class="nameSty">链接地址:</div>
            <el-input v-model="params.linkUrl" class="itpSty" type="number"></el-input>
        </div>
        <div class="flex_str_str itemSty">
            <div class="nameSty">上传图片:</div>
            <el-upload action="" v-model:file-list="fileList" list-type="picture-card"
                        :http-request="imgUpdate" >
                        <el-icon>
                            <Plus />
                        </el-icon>
                    </el-upload>
        </div>

        <template #footer>
            <span class="dialog-footer">
                <el-button @click="cancel">取消</el-button>
                <el-button type="primary" @click="submitFun">
                    确定
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script lang="ts" setup>
import { reactive, ref, toRefs, watch } from 'vue'
import { saveSwiperApi,updateFileApi } from '@/api/index/index'
import { ElMessage } from 'element-plus';
import type { UploadUserFile } from 'element-plus'
import { ElLoading } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
const props = defineProps({
    isShow: {
        type: Boolean,
        defaul: false
    }, id: {
        type: Number,
        default: null
    },
    rowData: {
        type: Object,
        default: {}
    }
})
const fileList = ref<UploadUserFile[]>([
])
const emit = defineEmits(['close'])
const { isShow, id, rowData } = toRefs(props)
const params = reactive({
    id:'',
    name: '',
    linkUrl: '',
    sort: '',
    type: '',
    drawingId:''
})
const cancel = () => {
    emit('close')
}
const submitFun = () => {
    saveSwiperApi(params).then((res: any) => {
        if (res.status == 200) {
            ElMessage.success(res.message)
            emit('close')
        }
    })
}
const openDialog = () => {
    // params.status = '1'
}
const imgUpdate = (uploadFile: any) => {
    const loading = ElLoading.service({
        lock: true,
        text: '上传中...',
        background: 'rgba(0, 0, 0, 0.7)',
    })
    const file = uploadFile.file;
    const formData = new FormData();
    formData.append("file", file);
    updateFileApi(formData).then((res: any) => {
        loading.close()
        params.drawingId += res.data.fileId + ','
    })
}
watch(() => rowData.value,
    (val) => {
        console.log('eiwojfioewe:',val)
        for (let i in val) {
            // @ts-ignore
            params[i] = val[i]
        }
    })

</script>
<style scoped>
.textSty {
    width: 400px;
    height: 170px;
    resize: none;
}

.dialog-footer button:first-child {
    margin-right: 10px;
}

.nameSty {
    width: 130px;
}

.itpSty {
    width: 300px;
}

.itemSty {
    margin: 10px 0;
}
</style>